/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package IU;

import Logica.Carrera;
import Logica.Fachada;
import Logica.Hipodromo;
import Logica.Jornada;
import Logica.Participante;
import Utilidades.ManejoFechas;
import Utilidades.Observable;
import Utilidades.Observador;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author jarzarello
 */
public class CarreraAbrir extends javax.swing.JDialog implements Observador {

    private final Hipodromo hs = PanelPrincipal.getInstancia().getHipodromoSeleccionado();
    private final ManejoFechas mf = new ManejoFechas();
    private Jornada jornadaHoy;
    
    public Hipodromo getHipodromo() {
        return hs;
    }

    public ManejoFechas getManejoFechas() {
        return mf;
    }

    public Jornada getJornadaHoy() {
        return jornadaHoy;
    }

    public void setJornadaHoy(Jornada jornadaHoy) {
        this.jornadaHoy = jornadaHoy;
    }
    
    /**
     * Creates new form CarreraAbrir
     */
    public CarreraAbrir(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Fachada.getInstancia().agregar(this);
        cargarCarreras();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblCarreras = new javax.swing.JLabel();
        lblHipodromo = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstCaballos = new javax.swing.JList();
        lblCaballosParticipantes = new javax.swing.JLabel();
        btnAbrir = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lblNumero = new javax.swing.JLabel();
        lblNombre = new javax.swing.JLabel();
        numero = new javax.swing.JLabel();
        nombre = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Abrir Carrera");

        lblCarreras.setText("Próxima carrera:");

        lstCaballos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(lstCaballos);

        lblCaballosParticipantes.setText("Lista de caballos participantes:");

        btnAbrir.setText("Abrir");
        btnAbrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAbrirActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        lblNumero.setText("N°:");

        lblNombre.setText("Nombre:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(lblCarreras, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(lblHipodromo))
                                .addComponent(jScrollPane2))
                            .addComponent(lblCaballosParticipantes)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblNumero)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(numero))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblNombre)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnAbrir, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 10, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblCarreras)
                    .addComponent(lblHipodromo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNumero)
                    .addComponent(numero))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombre)
                    .addComponent(nombre))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCaballosParticipantes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAbrir)
                    .addComponent(btnCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setBounds(0, 0, 304, 315);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAbrirActionPerformed
        // TODO add your handling code here:
        abrirCarrera();
    }//GEN-LAST:event_btnAbrirActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAbrir;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblCaballosParticipantes;
    private javax.swing.JLabel lblCarreras;
    private javax.swing.JLabel lblHipodromo;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblNumero;
    private javax.swing.JList lstCaballos;
    private javax.swing.JLabel nombre;
    private javax.swing.JLabel numero;
    // End of variables declaration//GEN-END:variables

    private void cargarCarreras() {
        
        //Date fechaHoy = mf.formatoFecha(new Date());

        Jornada j = hs.buscarJornada(new Date());
        
        Carrera carrera = j.buscarPrimera();
        ArrayList<String> participantes = new ArrayList();
        lblHipodromo.setText(hs.getNombre());
        
        if(carrera != null){
            
            numero.setText(String.valueOf(carrera.getNumero()));
            nombre.setText(carrera.getNombre());
            for(Participante p:carrera.getParticipantes()){
                participantes.add(formatearParticipantes(p));
            }
            lstCaballos.setListData(participantes.toArray());
        }else{
            JOptionPane.showMessageDialog(null, "No existen más carreras para el día de hoy.");
            numero.setText("");
            nombre.setText("");
            lstCaballos.removeAll();
        }   
    }

    private void abrirCarrera() {
        
        Date fechaHoy = mf.formatoFecha(new Date());
        
        Jornada j = hs.buscarJornada(fechaHoy);
        Carrera carrera = j.buscarPrimera(); //OBTENGO LA PROXIMA CARRERA PARA ABRIR
        
        int opcion = JOptionPane.showConfirmDialog(null, "Realmente desea abrir la carrera ?");
        if(opcion == JOptionPane.YES_OPTION){
            Fachada.getInstancia().abrirCarrera(carrera);
            //cargarCarreras();
        }
    }
    
    private String formatearParticipantes (Participante p) {
        return "Número: " + String.valueOf(p.getNumero()) + " - Nombre: " + p.getCaballo().getNombre()+ " - Dividendo: " + String.valueOf(p.getDividendo());
    }

    @Override
    public void actualizar(Observable origen, Object param) {
        if(param.equals(Fachada.Evento.CA_ABIERTA)){
            cargarCarreras();
        }
    }
}
